package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.ReportRelationshipMapper;
import com.tanhua.model.domain.Report;
import com.tanhua.model.domain.ReportRelationship;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 报告关系表的数据层实现类
 *
 * @author 李磊
 * @since 2022-01-15 15:34
 */
@DubboService
public class ReportRelationshipApiImpl implements ReportRelationshipApi {

    @Autowired
    ReportRelationshipMapper relationMapper;

    /**
     * 说明: 添加新的报告关系表
     *
     * @param relationshipList 关系集合
     * @param failedNumbers    失效的关系集合
     * @param userId           用户id
     * @author 李磊
     * @since 2022-01-15 21:28
     */

    @Override
    public void addSmallRelation(Long userId, List<ReportRelationship> relationshipList, ArrayList<Integer> failedNumbers) {

        if (failedNumbers.size() != 0) {
            //说明，总数已达10000，需要进行对应数据的删除
            LambdaQueryWrapper<ReportRelationship> deleteWrapper = new LambdaQueryWrapper<>();
            deleteWrapper.eq(ReportRelationship::getUserId, userId)
                    .in(ReportRelationship::getSimilarYouNumber, failedNumbers);
            relationMapper.delete(deleteWrapper);
        }
        //添加对应的数据
        LambdaQueryWrapper<ReportRelationship> addWrapper = new LambdaQueryWrapper<>();
        for (ReportRelationship relationship : relationshipList) {
            relationMapper.insert(relationship);
        }
    }

    /**
     * 说明:  查询用户的最进更新的.用户编号
     *
     * @param userId 用户id
     * @return int 最大编号
     * @author 李磊
     * @since 2022-01-15 21:44
     */
    @Override
    public int findMaxNumber(Long userId) {
        LambdaQueryWrapper<ReportRelationship> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReportRelationship::getUserId, userId).orderByDesc(ReportRelationship::getUpdated);
        //按照修改日期降序排列，只取第一页第一条
        Page<ReportRelationship> page = new Page<>(1, 1);
        Page<ReportRelationship> selectPage = relationMapper.selectPage(page, wrapper);
        List<ReportRelationship> records = selectPage.getRecords();
        //返回最大的用户编号

        if (CollUtil.isEmpty(records)) {
            //说明没有相似用户
            return 0;
        } else {
            return records.get(0).getSimilarYouNumber();
        }

    }

    /**
     * 说明:  查询和你相似的十条关系数据
     *
     * @param userId 用户id
     * @return java.util.List<com.tanhua.model.domain.ReportRelationship>
     * @author 李磊
     * @since 2022-01-16 00:01
     */
    @Override
    public List<ReportRelationship> findSimilarYou(Long userId) {

        LambdaQueryWrapper<ReportRelationship> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReportRelationship::getUserId, userId).orderByDesc(ReportRelationship::getUpdated);
        //按照修改日期降序排列，只取第一页第10条
        Page<ReportRelationship> page = new Page<>(1, 10);
        return relationMapper.selectPage(page, wrapper).getRecords();
    }
}

